package com.imy.luascript.view.navigation;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.imy.common.util.UiUtil;
import com.imy.luascript.R;
import com.imy.luascript.base.view.BaseCustomViewGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class NavigationView extends BaseCustomViewGroup {

    @BindView(R.id.tab_view_ll)
    LinearLayout tabViewLl;
    private int selectedTextColor;
    private int unSelectedTextColor;
    private int textSize;
    private int iconWidth;
    private int iconHeight;
    private int iconTextSpacing;
    private List<ImageView> imageViews=new ArrayList<>();
    private List<TextView> textViews =new ArrayList<>();
    public NavigationView(Context context) {
        super(context);
    }

    public NavigationView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attributeSet) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NavigationView, 0, 0);
        selectedTextColor = a.getColor(R.styleable.NavigationView_selected_text_color,
                mContext.getResources().getColor(R.color.default_green));
        unSelectedTextColor = a.getColor(R.styleable.NavigationView_un_selected_text_color,
                mContext.getResources().getColor(R.color.default_text_color));
        textSize = a.getDimensionPixelOffset(R.styleable.NavigationView_text_size, 13);
        iconWidth = a.getDimensionPixelOffset(R.styleable.NavigationView_icon_width, 20);
        iconHeight = a.getDimensionPixelOffset(R.styleable.NavigationView_icon_height, 20);
        iconTextSpacing = a.getDimensionPixelOffset(R.styleable.NavigationView_text_icon_spacing, 2);
        a.recycle();
    }

    private int currentPosition = 0;
    private ViewPager viewPager;
    public void bind(List<NavigationItem> list, ViewPager v) {
        this.viewPager=v;

        imageViews.clear();
        textViews.clear();
        tabViewLl.removeAllViews();
        for (int i = 0; i < list.size(); i++) {
            NavigationItem navigationItem = list.get(i);
            View view = View.inflate(mContext, R.layout.include_navigation_item, null);
            tabViewLl.addView(view);
            LinearLayout linearLayout = view.findViewById(R.id.include_navigation_ll);
            ViewGroup.LayoutParams layoutParams1 = linearLayout.getLayoutParams();
            layoutParams1.width=UiUtil.getDeviceWidth(mContext)/list.size();
            linearLayout.setLayoutParams(layoutParams1);

            ImageView imageView = view.findViewById(R.id.navigation_image);
            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
            layoutParams.width = iconWidth;
            layoutParams.height = iconHeight;
            imageView.setLayoutParams(layoutParams);
            TextView textView = view.findViewById(R.id.navigation_tv);
            MarginLayoutParams marginLayoutParams = (MarginLayoutParams) textView.getLayoutParams();
            marginLayoutParams.topMargin = iconTextSpacing;
            textView.setLayoutParams(marginLayoutParams);
            textView.setText(navigationItem.text);
            textView.setTextSize(textSize);
            if (currentPosition == i) {
                imageView.setImageResource(navigationItem.selectedRes);
                textView.setTextColor(selectedTextColor);
            } else {
                imageView.setImageResource(navigationItem.unSelectedRes);
                textView.setTextColor(unSelectedTextColor);
            }
            imageView.setTag(navigationItem);
            imageViews.add(imageView);
            textViews.add(textView);
            view.setTag(i);
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int i = (int) v.getTag();
                    PagerAdapter adapter = viewPager.getAdapter();
                    if (adapter!=null &&adapter.getCount()>i){
                        viewPager.setCurrentItem(i,false);
                    }
                    ImageView lastImage = imageViews.get(currentPosition);
                    NavigationItem tag = (NavigationItem) lastImage.getTag();
                    lastImage.setImageResource(tag.unSelectedRes);
                    TextView lastTv = textViews.get(currentPosition);
                    lastTv.setTextColor(unSelectedTextColor);
                    //当前的
                    ImageView currentImage = imageViews.get(i);
                    NavigationItem tag2 = (NavigationItem) currentImage.getTag();
                    currentImage.setImageResource(tag2.selectedRes);
                    TextView currentTv = textViews.get(i);
                    currentTv.setTextColor(selectedTextColor);
                    currentPosition=i;
                }
            });
        }
    }



    @Override
    protected int getLayoutID() {
        return R.layout.view_tab;
    }
}
